import wxPay from '@/utils/wxPay.js'

export default {
	pay: (trailData, isWechatMp, data, self, directUrl) => {

		if (data == '241039') {
			self.$tool.success('支付成功');
			setTimeout(() => {
				uni.redirectTo({
					url: directUrl
				})	
			}, 300)
			
			return ;
		}

		if (trailData.pay_way == 'wechat_pay') {
			//#ifdef H5
			if (isWechatMp) {
				wxPay.payment(data,
					(data) => {
						self.$tool.success('支付成功');
						uni.redirectTo({
							url: directUrl
						})
					},
					(e) => {
						self.$tool.error('支付失败');
						uni.redirectTo({
							url: directUrl
						})
					})
			} else {
				window.location.href = data
			}
			//#endif
			// #ifdef APP-PLUS
			uni.getProvider({
				service: 'payment',
				success: function (payProvider) {
					if (~payProvider.provider.indexOf('wxpay')) {
						uni.requestPayment({
							"provider": "wxpay",
							"orderInfo": {
								"appid": data.appId,
								"partnerid": data.partnerid,
								"prepayid": data.prepayid,
								"package": "Sign=WXPay",
								"noncestr": data.nonceStr,
								"timestamp": data.timeStamp,
								"sign": data.sign
							}, 
							success: function (payRes) {
								self.$tool.success('支付成功');
								uni.redirectTo({
									url: directUrl
								})
							},
							fail: function (err) {
								self.$tool.error('支付失败');
								uni.redirectTo({
									url: directUrl
								})
							}
						});
					}
				}
			});
			// #endif
			//#ifdef MP
			uni.requestPayment({
				"provider": "wxpay", 
				"appId": data.appId,
				"timeStamp": data.timeStamp,
				"nonceStr": data.nonceStr,
				"package": data.package,
				"signType": data.signType,
				"paySign": data.paySign,
				success(data) {
					self.$tool.success('支付成功');
					uni.redirectTo({
						url: directUrl
					})
				},
				fail(e) {
					self.$tool.error('支付失败');
					uni.redirectTo({
						url: directUrl
					})
				}
			})
			//#endif
		} else if (trailData.pay_way == 'alipay') {
			//#ifdef H5
			document.querySelector('body').innerHTML = data
			document.forms[0].submit()
			//#endif
			// #ifdef APP-PLUS
			uni.getProvider({
				service: 'payment',
				success: function (payProvider) {
					if (~payProvider.provider.indexOf('alipay')) {
						uni.requestPayment({
							"provider": "alipay",
							"orderInfo": data,
							success: function (payRes) {
								self.$tool.success('支付成功');
								uni.redirectTo({
									url: directUrl
								})
							},
							fail: function (err) {
								self.$tool.error('支付失败');
								uni.redirectTo({
									url: directUrl
								})
							}
						});
					}
				}
			});
			// #endif
		} else { // 余额支付
			if (data.code == 0) {
				self.$tool.success('支付成功');
				uni.redirectTo({
					url: directUrl
				})
			} else {
				self.$tool.error(data.msg);
			}
		}
	}
}